#R version 4.4.3 (2025-02-28 ucrt)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 11 x64 

library(dplyr) #1.1.4
library(ggplot2) #3.5.1
library(readr) #2.1.5
library(haven) #2.5.4
library(estimatr) #1.0.6
library(broom.mixed) #0.2.9.6
library(lme4) #1.1-37
library(countrycode) #1.6.1
library(forcats) #1.0.0
library(stargazer) #5.2.3
library(tibble) #3.2.1

devtools::install_github("dsself/standardizecountries")
library(standard)

`%notin%` <- function(x,y) !(x %in% y) 

setwd("C:/Users/selfd/Documents/autopartyinstitutionalization/legacy/cps/replication")

##Table 1####
gwf <- read_csv("data/gwf_panel.csv") 
wth <- read_csv("data/wth_panel.csv") 

d2 <- wth |> 
  filter(wth_regime %in% c("One-party", "Multi-party", "Military")) |> 
  select(countryname, wth_regime, wth_next, wth_number) |> 
  unique() |> 
  ungroup()

durationa_wth <- wth |> 
  filter(wth_regime %in% c("One-party", "Multi-party", "Military")) |> 
  select(countryname, wth_regime, wth_next, wth_number, wth_duration) |> 
  group_by(wth_regime) |> 
  summarize(duration = round(mean(wth_duration, na.rm = T), digits = 2))

total_wth <- count(d2, wth_regime) |> 
  mutate(p_total = round(n/sum(n),digits = 4)*100) |> 
  rename(total = n)

demo_wth <- d2 |> 
  group_by(wth_regime) |> 
  count(wth_regime, wth_next)|> 
  mutate(p_demo = round(n/sum(n), digits = 4)*100) |> 
  filter(wth_next == "Democracy") |> 
  rename(demo = n) |> 
  select(-wth_next)

fail_wth <- d2 |> 
  group_by(wth_regime) |> 
  count(wth_regime, wth_next)|> 
  mutate(p_nfail = round(n/sum(n), digits = 4)*100) |> 
  filter(wth_next != "not yet failed") |> 
  rename(fail = n) |> 
  select(-wth_next) |> 
  summarize_all(sum)

d2 <- wth |> 
  filter(wth_prior %in% c("One-party", "Multi-party", "Military") & wth_regime == "Democracy") |> 
  select(countryname, wth_regime, wth_prior, wth_duration, wth_fail, wth_number, wth_next)

demo_dur <- d2 |> 
  group_by(wth_prior) |> 
  summarize(duration_d = round(mean(wth_duration, na.rm = T), digits = 2)) |> 
  rename(wth_regime = wth_prior)

still_wth <- d2 |> 
  ungroup() |> 
  select(countryname, wth_prior, wth_next) |> 
  unique() |> 
  group_by(wth_prior) |> 
  count(wth_prior, wth_next) |> 
  mutate(p_still = round(n/sum(n), digits = 4)*100) |> 
  filter(wth_next == "not yet failed") |> 
  rename(still = n) |> 
  select(wth_regime = wth_prior, still, p_still, -wth_next)

wth <- left_join(total_wth, durationa_wth) |> 
  left_join(fail_wth) |> 
  left_join(demo_wth) |> 
  left_join(still_wth) |> 
  left_join(demo_dur) |> 
  rename(regime = wth_regime)

d2 <- gwf |> 
  filter(gwf_regime %in% c("Party", "Personal", "Military")) |> 
  select(countryname, gwf_regime, gwf_next, gwf_number) |> 
  unique()

durationa_gwf <- gwf |> 
  filter(gwf_regime %in% c("Party", "Personal", "Military")) |> 
  select(countryname, gwf_regime, gwf_next, gwf_number, gwf_duration) |> 
  group_by(gwf_regime) |> 
  summarize(duration = round(mean(gwf_duration, na.rm = T), digits = 2))

total_gwf <- d2 |> 
  ungroup() |> 
  count(gwf_regime) |> 
  mutate(p_total = round(n/sum(n),digits = 4)*100) |> 
  rename(total = n)

demo_gwf <- d2 |> 
  group_by(gwf_regime) |> 
  count(gwf_regime, gwf_next)|> 
  mutate(p_demo = round(n/sum(n), digits = 4)*100) |> 
  filter(gwf_next == "democracy") |> 
  rename(demo = n) |> 
  select(-gwf_next)

fail_gwf <- d2 |> 
  group_by(gwf_regime) |> 
  count(gwf_regime, gwf_next)|> 
  mutate(p_nfail = round(n/sum(n), digits = 4)*100) |> 
  filter(gwf_next != "not yet failed") |> 
  rename(fail = n) |> 
  select(-gwf_next) |> 
  summarize_all(sum)

d2 <- gwf |> 
  filter(gwf_prior %in% c("Party", "Personal", "Military") & gwf_regime == "democracy") |> 
  select(countryname, gwf_regime, gwf_prior, gwf_duration, gwf_fail, gwf_number, gwf_next)

demo_dur <- d2 |> 
  group_by(gwf_prior) |> 
  summarize(duration_d = round(mean(gwf_duration, na.rm = T), digits = 2)) |> 
  rename(gwf_regime = gwf_prior)

still_gwf <- d2 |> 
  select(countryname, gwf_prior, gwf_next) |> 
  unique() |> 
  group_by(gwf_prior) |> 
  count(gwf_prior, gwf_next) |> 
  mutate(p_still = round(n/sum(n), digits = 4)*100) |> 
  filter(gwf_next == "not yet failed") |> 
  rename(still = n) |> 
  select(gwf_regime = gwf_prior, still, p_still, -gwf_next)

gwf <- left_join(total_gwf, durationa_gwf) |> 
  left_join(fail_gwf) |> 
  left_join(demo_gwf) |> 
  left_join(still_gwf) |> 
  left_join(demo_dur) |> 
  rename(regime = gwf_regime)

table <- rbind(wth, gwf)

table <- as.data.frame(t(table)) |> 
  rownames_to_column("desc") |> 
  select(desc, 1, 2, 3, 4, 5, 6, 7)  |> 
  mutate(desc = c("Regime", "Total", "% Total", "Duration", "Failed", "% Fail", "Democratized", "% Democratized", 
                  "Still Democracy", "% Still Democracy", "Democratic Duration"))

colnames(table) <- c("Regime", "Military", "Multi-party", "One-party", "Military", "Party", "Personal")

table <- table[-1,]

stargazer(table, summary = F, rownames = F, dep.var.caption = "Dataset", column.labels   = c("WTH", "GWF"),
          column.separate = c(3, 3))

#Table 2 & 3####
gwf <- read_csv("data/gwf_panel.csv")
wth <- read_csv("data/wth_panel.csv")

asp <- read_csv("data/asp_leg.csv") |> 
  mutate(countryname = country_panel(country, year)) |> 
  mutate(vshare = ifelse(is.na(voteshare_nat), voteshare_const, voteshare_nat)) |> 
  dplyr::select(countryname, party, year, vshare) |> 
  dplyr::rename(partyname = party) |> 
  group_by(countryname, year) |> 
  dplyr::mutate(multiple = ifelse(dplyr::n_distinct(partyname) > 1, 1, 0)) |> 
  mutate(partyname = ifelse(multiple == 1, paste(countryname, "multiple", sep = " "), partyname)) |> 
  mutate(vshare = ifelse(multiple == 1, sum(vshare, na.rm = T), vshare)) |> 
  arrange(countryname, year) |> 
  mutate(asp = 1) |> 
  unique()  |>  
  filter(year <= 2010) |> 
  ungroup() |> 
  select(countryname, asp)

d1 <- gwf |> 
  ungroup() |> 
  filter(gwf_duration == 1, gwf_regime == "democracy", gwf_prior %in% c("Military", "Monarchy", "Party", "Personal",
                                                                        "foreign-occupied", "warlord", "warlord/foreign-occupied")) |> 
  select(countryname, year, v2xps_party, v2x_polyarchy, gwf_prior, gwf_regime, lgdp, gwf_pduration, gwf_number, fsu, ussr_sat, regime_elecs, av_dm) |> 
  na.omit() |> 
  left_join(asp, by = c("countryname")) |> 
  select(countryname, year, v2xps_party, gwf_prior, asp) |> 
  mutate(asp = ifelse(is.na(asp), 0, asp))|> 
  mutate_if(is.numeric, round, 2) |>  unique() 

d2 <- wth |> 
  filter(wth_duration == 1, !is.na(v2xps_party), wth_regime == "Democracy") |> 
  select(countryname, year, v2xps_party, v2x_polyarchy, wth_prior, lgdp, wth_pduration, wth_number, fsu, ussr_sat, regime_elecs, av_dm) |> 
  na.omit()|> 
  left_join(asp, by = c("countryname")) |> 
  select(countryname, year, v2xps_party, wth_prior, asp) |> 
  mutate(asp = ifelse(is.na(asp), 0, asp)) |> 
  mutate_if(is.numeric, round, 2) |>  unique()

#Table 2
stargazer(d1, summary = F, rownames = F, dep.var.caption = "Case Selection - GWF")
#Table 3
stargazer(d2, summary = F, rownames = F, dep.var.caption = "Case Selection - WTH")

#Figure 1####
d1 <- gwf |> 
  ungroup() |> 
  select(countryname, gwf_number, gwf_duration, v2xps_party, gwf_regime, gwf_prior, gwf_next) |> 
  mutate(smple = ifelse(gwf_regime == "democracy", 1, 0),
         smple = ifelse(gwf_next == "democracy", 1, smple),
         smple = ifelse(is.na(gwf_prior) & gwf_regime == "democracy", 0, smple),
         smple = ifelse(gwf_regime == "no regime", 0, smple)) |> 
  filter(smple == 1, gwf_duration <=15) |> 
  group_by(countryname, gwf_number) |> 
  mutate(time = ifelse(gwf_regime != "democracy", gwf_duration - max(gwf_duration), gwf_duration)) |> 
  mutate(time = ifelse(gwf_regime == "democracy", time - 1, time))  |> 
  filter(gwf_regime %in% c("democracy", "Personal", "Party", "Military", "Monarchy")) 

ggplot(d1)+ 
  geom_smooth(aes(time, v2xps_party, color = gwf_regime), se = FALSE, data = d1 |> filter(time <=0)) +
  geom_smooth(aes(time, v2xps_party, color = gwf_prior), se = FALSE, data = d1 |> filter(time > 0, gwf_prior %in% c("Military", "Personal", "Monarchy", "Party"))) +
  geom_vline(xintercept = 0) +
  scale_color_grey(name = "Regime") +
  xlab("Democratization") +
  ylab("Party Institutionalization") +
  guides(color=guide_legend(ncol=2)) +
  theme_bw() +
  theme(legend.position="bottom") 

#save to file
#ggsave(filename = "C:/Users/selfd/Documents/autopartyinstitutionalization/legacy/plots/appendix/appendix_a_fig1.jpg", dpi = 500, width = 5, height = 5)

#Factor Analysis----
library(zoo) #1.8-13
library(psych) #2.5.3

load("data/GWF_clean.rdta")
load("data/WTH_clean.rdta")


gwf <- filter(gwf, year >= 1970) |> 
  ungroup()
wth <- filter(wth, year >= 1970)

#load party data and identify ASPs####
d1 <- readRDS("data/vdem_party.rds") |> 
  mutate(countryname = country_panel(country_name, year)) |> 
  select(countryname, year, v2paenname, v2pashname, v2pagovsup, v2paseatshare, v2pavote, v2paclient, v2pasoctie, v2palocoff, v2paactcom, v2padisa, v2paind, v2panom) |> 
  mutate(v2panom = -1*v2panom, v2paind  = -1*v2paind, v2paclient = -1*v2paclient) |> 
  mutate(incumbent = ifelse(v2pagovsup == 0, 1, 0)) |> 
  mutate(asp = ifelse(countryname == "Albania" & v2pashname == "PSS", v2pashname, NA)) |> 
  mutate(asp = ifelse(countryname == "Bangladesh" & v2pashname == "BNP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Bolivia" & v2pashname == "ADN", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Brazil" & v2pashname %in% c("PDS", "PPB", "PFL"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Brazil" & v2pashname %in% c("PDS", "PPB", "PFL"), ("PDS + PPB + PFL"), NA)) |> 
  mutate(asp = ifelse(countryname == "Bulgaria" & v2pashname == "KzB/BSP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Central African Republic" & v2pashname == "RDC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Chile" & v2pashname %in% c("UDI", "RN"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Chile" & v2pashname %in% c("UDI", "RN"), ("UDI + RN"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Congo" & v2pashname == "PCT", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Croatia" & v2pashname == "SDP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Czech Republic" & v2pashname == "KSCM", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Dominican Republic" & v2pashname == "PRSC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "El Salvador" & v2pashname %in% c("ARENA", "PCN"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "El Salvador" & v2pashname %in% c("ARENA", "PCN"), ("ARENA + PCN"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Ghana" & v2pashname == "NDC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Guatemala" & v2pashname == "FRG", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Guinea-Bissau" & v2pashname == "PAIGC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Hungary" & v2pashname == "MSZP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Indonesia" & v2pashname == "Golkar", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Korea, Republic of" & v2pashname %in% c("DLP", "DJP", "MK/SH", "HD-SD", "LKP"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Korea, Republic of" & v2pashname %in% c("DLP", "DJP", "MK/SH", "HD-SD", "LKP"), ("DLP + DJP + MK/SH + HD-SD + LKP"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Lesotho" & v2pashname == "BNP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Lithuania" & v2pashname %in% c("LSDP", "LDDP"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Lithuania" & v2pashname %in% c("LSDP", "LDDP"), ("LSDP + LDDP"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "North Macedonia" & v2pashname == "SDSM", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Madagascar" & v2pashname == "AREMA", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Malawi" & v2pashname == "MCP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Mexico" & v2pashname == "PRI", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Moldova" & v2pashname == "PCRM", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Mongolia" & v2pashname == "MAN", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Montenegro" & v2pashname == "DPS", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Nepal" & v2pashname == "RPP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Nicaragua" & v2pashname == "FSLN", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Niger" & v2pashname == "MNSD-N", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Panama" & v2pashname == "PRD", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Paraguay" & v2pashname == "ANR-PC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Peru" & v2pashname %in% c("NM", "AF", "FP"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Peru" & v2pashname %in% c("NM", "AF", "FP"), ("NM + AF + FP"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Philippines" & v2pashname == "KBL", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Poland" & v2pashname == "SLD", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Rumania" & v2pashname %in% c("PD", "PSD"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Rumania" & v2pashname %in% c("PD", "PSD"), ("PD + PSD"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Senegal" & v2pashname == "PS/UPS", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Yugoslavia (Serbia)" & v2pashname == "SPS", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Sierra Leone" & v2pashname == "APC", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Slovakia" & v2pashname %in% c("SV", "SDL"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Slovakia" & v2pashname %in% c("SV", "SDL"), ("SV + SDL"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Slovenia" & v2pashname == "SD / ZLSD", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Spain" & v2pashname %in% c("AP / PP", "UCD"), v2pashname, asp)) |> 
  mutate(asp_multiple = ifelse(countryname == "Spain" & v2pashname %in% c("AP / PP", "UCD"), ("AP / PP + UCD"), asp_multiple)) |> 
  mutate(asp = ifelse(countryname == "Sri Lanka (Ceylon)" & v2pashname == "UNP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Taiwan" & v2pashname == "KMT", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Thailand" & v2pashname == "NAP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Turkey/Ottoman Empire" & v2pashname == "ANAP", v2pashname, asp)) |> 
  mutate(asp = ifelse(countryname == "Ukraine" & v2pashname == "KPU", v2pashname, asp)) |> 
  mutate(asp = ifelse(is.na(asp), "no asp", asp), asp_multiple = ifelse(is.na(asp_multiple), "no asp multi", asp_multiple))

#start off by identifying all the incumbent parties at the top, get their PI relative to all parties and then to other incumbent parties and then to others that become ASPs
#need to test whether party experience matters or if its going through the asp

#need to filter on authoritarian regimes for incubment PI
#then need to filter on ASPs for PI
d2 <- select(d1, countryname, year, asp, v2pashname, v2pagovsup, v2paclient, v2pasoctie, v2palocoff, v2panom) |> 
  na.omit() |> 
  mutate(v2panom = ((v2panom - min(v2panom, na.rm = T))/(max(v2panom, na.rm = T)-min(v2panom, na.rm = T)))) |> 
  mutate(v2pasoctie = ((v2pasoctie - min(v2pasoctie, na.rm = T))/(max(v2pasoctie, na.rm = T)-min(v2pasoctie, na.rm = T)))) |> 
  mutate(v2palocoff = ((v2palocoff - min(v2palocoff, na.rm = T))/(max(v2palocoff, na.rm = T)-min(v2palocoff, na.rm = T)))) 


#1st FA is incumbent parties relative to all parties####

fadf <- select(d2, v2pasoctie, v2palocoff, v2panom) 

fit1 <- factanal(fadf, 1, rotation="varimax", scores = "regression")

#Table 5 data
fit1

#pull data into df
outfa1 <- as_tibble(fit1$scores) |> 
  mutate(Factor1 = ((Factor1 - min(Factor1, na.rm = T))/(max(Factor1, na.rm = T)-min(Factor1, na.rm = T)))) |> 
  rename(pi1 = Factor1)

dfa <- cbind(d2, outfa1) |> 
  arrange(countryname, year) |> 
  filter(v2pagovsup == 0) |> 
  select(countryname, year, pi1)

#create a df that is just the authoritarian regimes and merge with the FA
df3 <- gwf |> 
  select(countryname, year, gwf_regime, gwf_prior, gwf_fail, gwf_number) |> 
  left_join(dfa, by = c("countryname", "year")) |> 
  group_by(countryname, gwf_number) |> 
  mutate(pi1 = ifelse(gwf_fail == 1, NA, pi1), pi1 = ifelse(gwf_fail == 1 & is.na(pi1), na.locf0(pi1), pi1)) |> 
  group_by(countryname) |> 
  mutate_all(~na.locf0(.)) |> 
  filter(gwf_regime %in% c("Monarchy", "Personal", "Military", "Party")) |> 
  select(countryname, year, pi1) |> 
  unique()

df4 <- wth |> 
  select(countryname, year, wth_regime, wth_prior, wth_fail, wth_number) |> 
  left_join(dfa, by = c("countryname", "year")) |> 
  filter(wth_regime != "Democracy") |> 
  group_by(countryname) |> 
  mutate_all(~na.locf0(.)) |> 
  select(countryname, year, pi1) |> 
  unique() 

gwf <- left_join(gwf, df3, by = c("countryname", "year")) |> 
  group_by(countryname, gwf_number) |> 
  mutate(pi1 = mean(pi1, na.rm = T)) |> 
  group_by(countryname) |> 
  mutate(pi1 = ifelse(lag(gwf_fail) == 1, lag(pi1), NA)) |> 
  group_by(countryname, gwf_number) |> 
  mutate(pi1 = first(pi1)) |> 
  mutate(pi1 = ifelse(gwf_prior %in% c("Military", "Personal", "Party", "Monarchy") & is.nan(pi1), 0, pi1)) |> 
  unique()

wth <- left_join(wth, df4, by = c("countryname", "year")) |> 
  group_by(countryname, wth_number) |> 
  mutate(pi1 = mean(pi1, na.rm = T)) |> 
  group_by(countryname) |> 
  mutate(pi1 = ifelse(lag(wth_fail) == 1, lag(pi1), NA)) |> 
  group_by(countryname, wth_number) |> 
  mutate(pi1 = first(pi1)) |> 
  mutate(pi1 = ifelse(wth_prior %in% c("Military", "Multi-party", "One-party", "Monarchy", "Other") & is.nan(pi1), 0, pi1)) |> 
  unique()

#2nd FA is PI of actual ASPs then link in ruling/reactive####
d3 <- read_csv("data/asp_full.csv") |> 
  mutate_at(vars(ruling, alt_ruling), ~ ifelse(asp_dummy == 0, NA, .))

fadf <- select(d2, v2pasoctie, v2palocoff, v2panom) 

fit1 <- factanal(fadf, 1, rotation="varimax", scores = "regression")

outfa1 <- as_tibble(fit1$scores) |> 
  mutate(Factor1 = ((Factor1 - min(Factor1, na.rm = T))/(max(Factor1, na.rm = T)-min(Factor1, na.rm = T)))) |> 
  rename(pi4 = Factor1)

dfa <- cbind(d2, outfa1) |> 
  arrange(countryname, year) |> 
  filter(asp != "no asp")

df3 <- gwf |> 
  select(countryname, year, gwf_regime, gwf_prior, gwf_fail) |> 
  left_join(dfa, by = c("countryname", "year")) |> 
  group_by(countryname, year) |> 
  mutate(pi4 = mean(pi4, na.rm = T)) |> 
  group_by(countryname) |> 
  mutate_all(~na.locf0(.)) |> 
  filter(gwf_prior %in% c("Monarchy", "Personal", "Military", "Party")) |> 
  select(countryname, year, pi4) |> 
  unique()

gwf <- left_join(gwf, df3, by = c("countryname", "year")) |> 
  left_join(d3, by = c("countryname", "year")) |> 
  mutate_at(vars(asp_dummy, ruling, alt_ruling), ~na.locf0(.)) |> 
  unique() |> 
  mutate(pi4 = ifelse(asp_dummy == 0, NA, pi4))

fadf <- select(d2, v2pasoctie, v2palocoff, v2panom) 

fit1 <- factanal(fadf, 1, rotation="varimax", scores = "regression")

outfa1 <- as_tibble(fit1$scores) |> 
  mutate(Factor1 = ((Factor1 - min(Factor1, na.rm = T))/(max(Factor1, na.rm = T)-min(Factor1, na.rm = T)))) |> 
  rename(pi4 = Factor1)

dfa <- cbind(d2, outfa1) |> 
  arrange(countryname, year) |> 
  filter(asp != "no asp")

df4 <- wth |> 
  select(countryname, year, wth_regime, wth_prior, wth_fail) |> 
  left_join(dfa, by = c("countryname", "year")) |> 
  group_by(countryname, year) |> 
  mutate(pi4 = mean(pi4, na.rm = T)) |>   
  group_by(countryname) |> 
  mutate_all(~na.locf0(.)) |> 
  filter(wth_regime == "Democracy") |> 
  select(countryname, year, pi4) |> 
  unique()

wth <- left_join(wth, df4, by = c("countryname", "year")) |> 
  unique() |> 
  left_join(d3, by = c("countryname", "year")) |> 
  mutate_at(vars(asp_dummy, ruling, alt_ruling), ~na.locf0(.)) |> 
  mutate(pi4 = ifelse(asp_dummy == 0, NA, pi4)) |> 
  ungroup()


#Table 6 data
summary(gwf$pi1)
summary(gwf$pi4)
sd(gwf$pi1, na.rm = T)
sd(gwf$pi4, na.rm = T)
summary(wth$pi1)
summary(wth$pi4)
sd(wth$pi1, na.rm = T)
sd(wth$pi4, na.rm = T)
